package com.study.controller;

import com.study.entity.Role;
import com.study.entity.RolePermission;
import com.study.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/selectRoleInfos")
    public ModelAndView getRoleInfo() {
        ModelAndView mv = new ModelAndView();
        List<Role> roleList = roleService.selectAllRole();
        mv.addObject("roleList", roleList);
        return mv;
    }

    //------------------todo
    // 角色管理

    /**
     * 显示所有的角色信息
     * 数据 视图
     */
    @RequestMapping(value = "/roleManagement",method = RequestMethod.GET)
    public ModelAndView showRoleInfo() {
        ModelAndView mv = new ModelAndView();
        List<Role> roleList = roleService.selectAllRole();
        mv.addObject("roleList", roleList);
        mv.setViewName("role-list");
        return mv;
    }

    @RequestMapping(value = "/addRole",method = RequestMethod.GET)
    public String addRole() {
        return "role-add";
    }

    // 新增角色
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    public String addRole(Role role) {
        roleService.addRole(role);
        return "redirect:/role/roleManagement";
    }

    // 删除角色
    @RequestMapping(value = "/deleteRole",method = RequestMethod.POST)
    @ResponseBody
    public void deleteRole(Integer roleId) {
        roleService.deleteRole(roleId);
    }

    @RequestMapping(value = "/selectRolePermission")
    @ResponseBody
    public Role selectRolePermission(Integer roleId) {
        return roleService.selectRolePermission(roleId);
    }

    @RequestMapping(value = "/saveAllocatedPermission")
    @ResponseBody
    public void saveAllocatedPermission(Integer roleId, String resIdBatches) {
        String[] resIds = resIdBatches.split(",");
        roleService.saveAllocatedPermission(roleId,resIds);
    }
}
